package com.zh.chat.gui;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import com.zh.chat.controller.ClientContext;
import com.zh.chat.entity.User;
import com.zh.chat.gui.datechooser.DateChooserJButton;

/**
 * 用户详细信息显示框
 * 
 * @author 逍遥の剑仙 2013-3-6
 */
public class RegistFrame extends javax.swing.JFrame {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JButton registBtn; // 确定按钮
	private DateChooserJButton dateChooserBtn; // 选择日期按钮
	private JPanel birthdayPanel; // 生日栏
	private JPanel sexPanel; // 性别选择面板
	private JRadioButton sexFRb; // 性别选择按钮组
	private ButtonGroup sexRB;
	private JRadioButton sexMRb;
	private JTextField nameTF; // 姓名JTextField
	private JTextField registDateTF; // 注册时间JTextField
	private JTextField nickNameTF; // 昵称JTextField
	private JPasswordField passwordTF;//密码JTextField
	private JPanel infoTextPanel;
	private JPanel btnPanel;
	private JButton cancelBtn;
	private JLabel rigisterDateLb;
	private JLabel nameLb;
	private JLabel sexLb;
	private JLabel birthdayLb;
	private JLabel nickNameLb;
	private JLabel sysIdLb;
	private JLabel passwordLb;
	// 以下是系统关联对象
	private ClientContext clientContext = null;
	private User user = null;
	
	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}

	public RegistFrame() {
		super();
		initGUI();
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					clientContext.getRegistFrame().dispose();
				} catch (RuntimeException e2) {}
			}
		});
	}
	public static void main(String[] args) {
		RegistFrame frame = new RegistFrame();
		frame.setVisible(true);
	}
	private void initGUI() {
		try {
			// 设置左上角的图标
			URL url = this.getClass().getClassLoader().getResource("com/zh/chat/image/icon/32px.png");
			setIconImage(Toolkit.getDefaultToolkit().createImage(url));
			{
				getContentPane().setLayout(null);
				getContentPane().setForeground(new java.awt.Color(0, 0, 0));
				getContentPane().setBackground(new java.awt.Color(223, 228, 255));
				this.setPreferredSize(new java.awt.Dimension(500, 300));
				this.setLocationRelativeTo(null);
				{
					btnPanel = new JPanel();
					FlowLayout btnPanelLayout = new FlowLayout(FlowLayout.RIGHT);
					getContentPane().add(btnPanel);
					btnPanel.setBounds(0, 245, 345, 35);
					btnPanel.setBackground(new java.awt.Color(136, 199, 251));
					btnPanel.setBorder(new LineBorder(new java.awt.Color(147,
							234, 255), 1, false));
					btnPanel.setLayout(btnPanelLayout);
					{
						registBtn = new JButton();
						btnPanel.add(registBtn);
						registBtn.setText("\u6CE8\u518C");
						registBtn.setPreferredSize(new java.awt.Dimension(76, 23));
						registBtn.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseClicked(MouseEvent e) {
								String error = validateInput();
								if(error.equals("null")){
									user = new User();
									user.setBirthday(dateChooserBtn.getDate());
									user.setName(nameTF.getText());
									user.setNickname(nickNameTF.getText());
									user.setUserPwd(new String(passwordTF.getPassword()));
									if (sexFRb.isSelected()) {
										user.setSex("F");
									} else if (sexMRb.isSelected()) {
										user.setSex("M");
									}
									clientContext.registUser(user);								
								}else{
									JOptionPane.showMessageDialog(null , error);
								}
							}
						});
					}
					{
						cancelBtn = new JButton();
						btnPanel.add(cancelBtn);
						cancelBtn.setText("\u53d6\u6d88");
						cancelBtn.setPreferredSize(new java.awt.Dimension(76,23));
						cancelBtn.addMouseListener(new MouseAdapter() {
							// TODO 点击取消按钮关闭窗口
							@Override
							public void mouseClicked(MouseEvent e) {
								clientContext.getRegistFrame().dispose();
							}
						});
					}
				}
				{
					infoTextPanel = new JPanel();
					GridBagLayout infoTextPanelLayout = new GridBagLayout();
					infoTextPanelLayout.columnWidths = new int[] { 93, 7 };
					infoTextPanelLayout.rowHeights = new int[] { 10, 10, 10,
							10, 10, 10, 10 };
					infoTextPanelLayout.columnWeights = new double[] { 0.0, 0.1 };
					infoTextPanelLayout.rowWeights = new double[] { 0.1, 0.1,
							0.1, 0.1, 0.1, 0.1, 0.1 };
					getContentPane().add(infoTextPanel);
					infoTextPanel.setLayout(infoTextPanelLayout);
					infoTextPanel.setBounds(2, 0, 279, 240);
					infoTextPanel.setOpaque(false);
					{
						nickNameLb = new JLabel();
						infoTextPanel.add(nickNameLb, new GridBagConstraints(0,
								1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						nickNameLb.setText("\u6635\u79f0");
						nickNameLb.setBounds(305, 35, 48, 15);
					}
					{
						nameLb = new JLabel();
						infoTextPanel.add(nameLb, new GridBagConstraints(0, 2,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						nameLb.setText("\u59d3\u540d");
						nameLb.setBounds(305, 60, 48, 15);
					}
					{
						sexLb = new JLabel();
						infoTextPanel.add(sexLb, new GridBagConstraints(0, 3,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						sexLb.setText("\u6027\u522b");
						sexLb.setBounds(305, 86, 48, 15);
					}
					{
						passwordLb = new JLabel();
						infoTextPanel.add(passwordLb, new GridBagConstraints(0, 6,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE, new Insets(0, 0,
										0, 0), 0, 0));
						passwordLb.setText("\u5BC6\u7801");
						passwordLb.setBounds(305, 60, 48, 25);
					}
					{
						birthdayLb = new JLabel();
						infoTextPanel.add(birthdayLb, new GridBagConstraints(0,
								4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						birthdayLb.setText("\u751f\u65e5");
						birthdayLb.setBounds(305, 111, 48, 15);
					}
					{
						rigisterDateLb = new JLabel();
						infoTextPanel.add(rigisterDateLb,
								new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.NONE, new Insets(0,
												0, 0, 0), 0, 0));
						rigisterDateLb.setText("\u6ce8\u518c\u65f6\u95f4");
						rigisterDateLb.setBounds(305, 136, 48, 15);
					}
					{
						sysIdLb = new JLabel("\u6CE8\u518C\u4FE1\u606F");
						sysIdLb.setFont(new Font("黑体", Font.BOLD, 18));
						infoTextPanel.add(sysIdLb, new GridBagConstraints(1, 0,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 35,
										0, 0), 0, 0));
					}
					{
						nickNameTF = new JTextField();
						infoTextPanel.add(nickNameTF, new GridBagConstraints(1,
								1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
					}
					{
						nameTF = new JTextField();
						infoTextPanel.add(nameTF, new GridBagConstraints(1, 2,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
					}
					{
						registDateTF = new JTextField();
						infoTextPanel.add(registDateTF, new GridBagConstraints(
								1, 5, 1, 1, 0.0, 0.0,
								GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
						registDateTF.setEditable(false);
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						registDateTF.setText(sdf.format(new Date()));
					}
					{
						sexPanel = new JPanel();
						infoTextPanel.add(sexPanel, new GridBagConstraints(1,
								3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.BOTH,
								new Insets(0, 0, 0, 0), 0, 0));
						sexPanel.setLayout(null);
						sexRB = new ButtonGroup();
						sexPanel.setSize(193, 20);
						sexPanel.setOpaque(false);
						{
							sexFRb = new JRadioButton();
							sexPanel.add(sexFRb);
							sexRB.add(sexFRb);
							sexFRb.setText("\u5973");
							sexFRb.setBounds(0, 0, 50, 20);
							sexFRb.setOpaque(false);

						}
						{
							sexMRb = new JRadioButton();
							sexPanel.add(sexMRb);
							sexRB.add(sexMRb);
							sexMRb.setText("\u7537");
							sexMRb.setBounds(47, 0, 50, 20);
							sexMRb.setOpaque(false);
						}
					}
					{
						birthdayPanel = new JPanel();
						infoTextPanel.add(birthdayPanel,
								new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.BOTH, new Insets(0,
												0, 0, 0), 0, 0));
						birthdayPanel.setLayout(null);
						birthdayPanel.setOpaque(false);
						{
							dateChooserBtn = new DateChooserJButton();
							birthdayPanel.add(dateChooserBtn);
							dateChooserBtn.setDate(new Date());
							dateChooserBtn.setBounds(0, 0, 186, 30);
						}
					}
					{
						passwordTF = new JPasswordField();
						infoTextPanel.add(passwordTF, new GridBagConstraints(1, 6,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
					}
				}
			}
			this.setSize(350, 310);
			this.setResizable(false);
			this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private String validateInput(){
		String nickName = nickNameTF.getText().trim();
		String nameStr = nameTF.getText().trim();
		if(nickName.equals("")){
			return "昵称没有填写";
		}else if(nameStr.equals("")){
			return "姓名没有填写";
		}else if(sexRB.getSelection()==null){
			return "没有选择性别";
		}else if(new String(passwordTF.getPassword()).equals("")){
			return "密码没有填写";
		}
		return "null";
	}
}
